package com.objcat.playground.leetcode;

/**
 * description: Solution27 <br>
 * date: 2022/3/21 9:45 AM <br>
 * author: objcat <br>
 * version: 1.0 <br>
 */
public class Solution27 {
    /**
     * description: isSymmetric <br>
     * version: 1.0 <br>
     * date: 2022/3/21 5:00 AM <br>
     * author: objcat <br>
     * @param root
     * @return boolean
     * 执行用时：0 ms, 在所有 Java 提交中击败了 100 % 的用户
     * 内存消耗：39.7 MB, 在所有 Java 提交中击败了 21.09 % 的用户
     */
    public boolean isSymmetric(TreeNode root) {
        // 从第二层开始比较
        return isSymmetric(root.left, root.right);
    }

    public boolean isSymmetric(TreeNode left, TreeNode right) {
        // 如果都为空说明相等
        if (left == null && right == null) {
            return true;
        }
        // 如果单一节点为空或节点的值不同则说明不对称
        if (left == null || right == null || left.val != right.val) {
            return false;
        }
        // 左节点的左边和右节点的右边进行比较 && 左节点的右边和右节点的左边进行比较
        return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(2);
        node1.left = node2;
        node1.right = node3;
        Solution27 solution27 = new Solution27();
        System.out.println(solution27.isSymmetric(node1));
    }
}
